package com.dfc.springmvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.dfc.springmvc.common.Criteria;
import com.dfc.springmvc.pojo.DictionaryDetail;
import com.dfc.springmvc.pojo.PhoneRepairPrice;
import com.dfc.springmvc.service.PriceService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping(value = "/price")
public class PriceController extends BaseController {

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private PriceService priceService;

	@RequestMapping(value = "/initPrice", method = RequestMethod.GET)
	public String loadDictionary() {
		return "/price/priceManage";
	}
	
	@RequestMapping(value = "/newStory", method = RequestMethod.GET)
	public String newStory() {
		return "/price/index";
	}
	
	/**
	 * 查询用户信息
	 * 
	 * @param model
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping(value = "/queryPrice")
	@ResponseBody
	public Map<String, Object> queryUser(@RequestParam(required=false)String phoneType,
			@RequestParam(required=false)String detailType,
			@RequestParam(required=false)String repairType,
			int page, int rows) {
		log.info("查询维修价格列表");
		Map<String, Object> map = new HashMap<String, Object>();
		Criteria criteria = new Criteria();

		int pageSize = rows == 0 ? 20 : rows;
		int currPage = page == 0 ? 1 : page;
		int start = (currPage - 1) * pageSize;
		
		criteria.setStart(start);
		criteria.setLimit(pageSize);
		criteria.put("start", start);
		criteria.put("limit", 20);
		if(StringUtils.isNotBlank(phoneType)){
			criteria.put("phoneType", StringUtils.trim(phoneType));
		}
		if(StringUtils.isNotBlank(detailType)){
			criteria.put("detailType", StringUtils.trim(detailType));
		}
		if(StringUtils.isNotBlank(repairType)){
			criteria.put("repairType", StringUtils.trim(repairType));
		}
		
		
		List<PhoneRepairPrice> list = priceService.findPhonePriceList(criteria);
		int total = priceService.getTotalCount(criteria);
		
		if(list != null && list.size() > 0){
			map.put("total", total);
			map.put("rows", list);
		}else{
			map.put("total", 0);
			map.put("rows", list);
		}
		return map;
	}
	
	@RequestMapping(value="/savePhonePrice")
	@ResponseBody
	public Map<String, String> savePhonePrice(PhoneRepairPrice phonePrice){
		Map<String, String> map = new HashMap<String, String>();
		int flag = 0;
		if(StringUtils.isNotBlank(phonePrice.getPriceId())){
			flag = priceService.updatePhoneprice(phonePrice);
			if(flag == 1){
				map.put("message", "修改成功");
				map.put("result", "true");
			}else{
				map.put("message", "修改失败");
				map.put("result", "false");
			}
		}else{
			flag = priceService.savePhonePrice(phonePrice);
			if(flag == 1){
				map.put("message", "保存成功");
				map.put("result", "true");
			}else{
				map.put("message", "保存失败");
				map.put("result", "false");
			}
		}
		
		return map;
	}
	
	@RequestMapping(value="/delPhonePrice")
	@ResponseBody
	public Map<String, String> delPhonePrice(String phonePriceId){
		Map<String, String> map = new HashMap<String, String>();
		int flag = priceService.delPhonePrice(phonePriceId);
		if(flag == 1){
			map.put("message", "删除成功");
			map.put("result", "true");
		}else{
			map.put("message", "删除失败");
			map.put("result", "false");
		}
		return map;
	}
	
	/**
	 * 查询用户信息
	 * 
	 * @param model
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping(value = "/queryPhonePrice")
	@ResponseBody
	public Map<String, String> queryPhonePrice(@RequestParam String phoneTypeId,
			@RequestParam String detailTypeCode,
			@RequestParam String repairTypeCode) {
		log.info("查询维修价格列表");
		Map<String, String> map = new HashMap<String, String>();
		Criteria criteria = new Criteria();
		if(StringUtils.isNotBlank(phoneTypeId)){
			criteria.put("phoneTypeId", StringUtils.trim(phoneTypeId));
		}
		if(StringUtils.isNotBlank(detailTypeCode)){
			criteria.put("detailTypeCode", StringUtils.trim(detailTypeCode));
		}
		if(StringUtils.isNotBlank(repairTypeCode)){
			criteria.put("repairTypeCode", StringUtils.trim(repairTypeCode));
		}
		
		
		PhoneRepairPrice phoneRepairPrice = priceService.findPhonePrice(criteria);
		if(phoneRepairPrice != null){
			map.put("price", phoneRepairPrice.getRepairPrice().setScale(2).toPlainString());
		}
		
		return map;
	}
	
	/**
	 * 获取手机明细项类型
	 * @param dictionaryDetail
	 * @return
	 */
	@RequestMapping(value = "/getPhoneType", method = RequestMethod.POST)
	@ResponseBody
	public JSONArray getPhoneType() {
		JSONArray dictList = new JSONArray();
		try{
			List<PhoneRepairPrice> repairPriceList = priceService.getPhoneType();
			
			for(PhoneRepairPrice repairPrice : repairPriceList){
				JSONObject json = new JSONObject();
				json.put("text", repairPrice.getPricePhoneText());
				json.put("id", repairPrice.getPricePhoneId());
				dictList.add(json);
			}
			
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return dictList;
	}
	
	/**
	 * 获取手机明细项类型
	 * @param dictionaryDetail
	 * @return
	 */
	@RequestMapping(value = "/getPhoneDetailType", method = RequestMethod.POST)
	@ResponseBody
	public JSONArray getPhoneDetailType(String dictId) {
		JSONArray dictList = new JSONArray();
		try{
			List<PhoneRepairPrice> repairPriceList = priceService.getDictDetailByDictId(dictId);
			
			for(PhoneRepairPrice repairPrice : repairPriceList){
				JSONObject json = new JSONObject();
				json.put("text", repairPrice.getPriceDetailText());
				json.put("id", repairPrice.getPriceDetailId());
				dictList.add(json);
			}
			
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return dictList;
	}
	
	/**
	 * 获取手机明细项类型
	 * @param dictionaryDetail
	 * @return
	 */
	@RequestMapping(value = "/getPhoneRepairType", method = RequestMethod.POST)
	@ResponseBody
	public JSONArray getPhoneRepairType(String dictId) {
		JSONArray dictList = new JSONArray();
		try{
			List<PhoneRepairPrice> repairPriceList = priceService.getDictDetailByDictId(dictId);
			
			for(PhoneRepairPrice repairPrice : repairPriceList){
				JSONObject json = new JSONObject();
				json.put("text", repairPrice.getPriceRepairText());
				json.put("id", repairPrice.getPriceRepairId());
				dictList.add(json);
			}
			
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return dictList;
	}

}
